

<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>3. 存储和使用变量 &mdash; AIL-docs  文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="4. 基本AIL程序" href="program.html" />
    <link rel="prev" title="2. 将AIL用于计算" href="basic.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> AIL-docs
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../whatsnew/index.html">版本介绍</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">入门教程</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="setup.html">1. 搭建AIL运行环境</a></li>
<li class="toctree-l2"><a class="reference internal" href="basic.html">2. 将AIL用于计算</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">3. 存储和使用变量</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#id2">3.1. 定义与赋值</a></li>
<li class="toctree-l3"><a class="reference internal" href="#id3">3.2. 使用变量</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#id4">3.2.1. 使用变量进行运算</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id5">3.2.2. 无意义代码</a></li>
<li class="toctree-l4"><a class="reference internal" href="#id6">3.2.3. 作为别称</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#id7">3.3. 小结</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="program.html">4. 基本AIL程序</a></li>
<li class="toctree-l2"><a class="reference internal" href="basicstmt.html">5. 基本输入输出语句</a></li>
<li class="toctree-l2"><a class="reference internal" href="controlflow.html">6. 基本流程控制语句</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../library/index.html">标准库参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">语言参考</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">AIL-docs</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="index.html">AIL 教程</a> &raquo;</li>
        
      <li><span class="section-number">3. </span>存储和使用变量</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/tutorial/variable.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="id1">
<h1><span class="section-number">3. </span>存储和使用变量<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h1>
<div class="section" id="id2">
<h2><span class="section-number">3.1. </span>定义与赋值<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h2>
<p>变量是编程中十分重要的存在。没有变量，那么一切操作都会变得十分痛苦。</p>
<p>AIL作为一门高级程序语言，自然也提供这方面的支持。</p>
<p>若要 <strong>定义</strong> 一个变量，只要在变量名和变量值之间，使用等号(<code class="code docutils literal notranslate"><span class="pre">=</span></code>)相连，就像这样：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
</pre></div>
</div>
<p>这样会发生什么？虽然在交互模式下输入这行代码，按下回车后，你发现什么也没有发生，AIL既不会自动卸载，天上也不会掉下女朋友。但事实是，通俗来讲， <strong>AIL存储了变量a</strong> 。那么，如何访问这个变量呢？只需要敲入名字即可。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">...</span>
<span class="o">&gt;&gt;</span> <span class="n">a</span>
<span class="o">&lt;</span> <span class="mi">0</span> <span class="o">&gt;</span>
</pre></div>
</div>
<p>a的值被打印出来了。</p>
<p><strong>切记，不要访问一个不存在的变量！</strong></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">b</span>
<span class="o">&gt;&gt;</span> <span class="n">b</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s1">&#39;&lt;shell&gt;&#39;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">2</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">main</span><span class="o">&gt;</span>
<span class="ne">NameError</span><span class="p">:</span> <span class="n">name</span> <span class="s1">&#39;b&#39;</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">defined</span>
</pre></div>
</div>
<p>那么问题来了，若再执行一遍: <code class="code docutils literal notranslate"><span class="pre">a</span> <span class="pre">=</span> <span class="pre">0</span></code> ，会发生什么？因为AIL已经被存储一遍了，为了不让本章阅读难度提升，通俗来讲，这个操作让AIL修改了a的值，但修改后仍然为0。要想看看a的变化，不妨试试别的值</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span>
<span class="o">&gt;&gt;</span> <span class="n">a</span>
<span class="o">&lt;</span> <span class="mi">0</span> <span class="o">&gt;</span>
<span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">3</span>
<span class="o">&lt;</span> <span class="mi">3</span> <span class="o">&gt;</span>
</pre></div>
</div>
<p>可以发现，a的值的确被修改了。</p>
<p>这种操作，叫做 <strong>赋值</strong> 。</p>
<p>问题来了，AIL的变量可以存储什么？刚才的演示，存储的是数字。实际上，（不严格来说）AIL的变量可以存储 <strong>任何东西</strong> ，包括字符串，数组，函数，结构体和对象等等…</p>
</div>
<div class="section" id="id3">
<h2><span class="section-number">3.2. </span>使用变量<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>使用变量，只需要键入其变量名即可，这是对变量最基本的操作（赋值比这更高级）。但是，光键入变量名还不够。还需要将变量投入使用。</p>
<div class="section" id="id4">
<h3><span class="section-number">3.2.1. </span>使用变量进行运算<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h3>
<p>在实际使用的时候，我们可以把变量看做其变量的值，来进行运算。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
<span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">1</span>
<span class="o">&lt;</span> <span class="mi">3</span> <span class="o">&gt;</span>
<span class="o">&gt;&gt;</span> <span class="s1">&#39;Nezha&#39;</span><span class="p">[</span><span class="n">a</span><span class="p">]</span>
<span class="s1">&#39;z&#39;</span>
</pre></div>
</div>
<p>还可以对变量进行其值的类型所具有的操作，如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;Nezha&#39;</span>
<span class="o">&gt;&gt;</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="s1">&#39;N&#39;</span>
<span class="o">&gt;&gt;</span> <span class="n">name</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="s1">&#39;a&#39;</span>
<span class="o">&gt;&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="o">&lt;</span> <span class="mi">5</span> <span class="o">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="id5">
<h3><span class="section-number">3.2.2. </span>无意义代码<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h3>
<p>何为 <em>无意义</em> 代码？就是没有任何效果的代码。在Java这类编程语言中，无意义代码是不允许存在的。</p>
<p>比如在Java程序中，下面的内容就属于无意义代码：</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="p">...</span>
<span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">a</span>  <span class="c1">// no effect.</span>
<span class="p">...</span>
</pre></div>
</div>
<p>为什么没有意义？因为没有任何作用效果，不会影响程序运行。</p>
<p>在AIL中，虽然是“纵容”无意义代码的存在，但是还是少写这类代码。否则会对性能产生影响。</p>
<p>若要问：在交互环境中，我键入 a ，这属于无意义代码吗？
答案是否定的，因为它有意义。（至少让你知道a的值是什么或者它存不存在）。</p>
</div>
<div class="section" id="id6">
<h3><span class="section-number">3.2.3. </span>作为别称<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h3>
<p>AIL的变量本质上，就是持有对值的引用。通俗来讲，就是打标签。所以说利用这一特性，可以干一些别的事情。</p>
<p><em>在后续有关函数的章节中，我们会认识到函数名也是变量名</em></p>
<p>比如，可以创建某个函数的别称：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">&gt;&gt;</span> <span class="n">get_length</span> <span class="o">=</span> <span class="nb">len</span>
<span class="o">&gt;&gt;</span> <span class="n">get_length</span><span class="p">(</span><span class="s1">&#39;Nezha&#39;</span><span class="p">)</span>
<span class="o">&lt;</span> <span class="mi">5</span> <span class="o">&gt;</span>
<span class="o">&gt;&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="s1">&#39;Nezha&#39;</span><span class="p">)</span>
<span class="o">&lt;</span> <span class="mi">5</span> <span class="o">&gt;</span>
</pre></div>
</div>
<p>这样对于一些比较难写，但是常用的函数来说，这是很方便的。比如内置函数 <code class="code docutils literal notranslate"><span class="pre">_get_ccom</span></code> ，可以执行 <code class="code docutils literal notranslate"><span class="pre">get_ccom</span> <span class="pre">=</span> <span class="pre">_get_ccom</span></code> ，这样在使用函数的时候，就不用翻山越岭去找到那个下划线了。</p>
<p><em>要是问为啥不直接在语言里面把 _get_ccom 改成 get_ccom ，回答就是: …</em></p>
</div>
</div>
<div class="section" id="id7">
<h2><span class="section-number">3.3. </span>小结<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="program.html" class="btn btn-neutral float-right" title="4. 基本AIL程序" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="basic.html" class="btn btn-neutral float-left" title="2. 将AIL用于计算" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2020, ChenHongbo

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>